using System;
using NUnit.Framework;
using ValidationFramework.Reflection;

namespace ValidationFramework.Tests
{
    public class RequiredStringRuleTest
    {
        
        [TestFixture]
        public class IsEquivalent
        {

            [Test]
            public void Simple()
            {
                var rule1 = new RequiredStringRule
                            	{
                            		InitialValue = "a",
                                    IgnoreCase = true,
                                    TrimWhiteSpace = true
                            	};
				var rule2 = new RequiredStringRule
				{
					InitialValue = "a",
					IgnoreCase = true,
					TrimWhiteSpace = true
				};
                Assert.IsTrue(rule1.IsEquivalent(rule2));


				rule1 = new RequiredStringRule
				{
					InitialValue = "a",
					IgnoreCase = true,
					TrimWhiteSpace = true
				};
				rule2 = new RequiredStringRule
				{
					InitialValue = "a",
					IgnoreCase = true,
					TrimWhiteSpace = false
				};
                Assert.IsFalse(rule1.IsEquivalent(rule2));

				rule1 = new RequiredStringRule
				{
					InitialValue = "a",
					IgnoreCase = true,
					TrimWhiteSpace = true
				};
				rule2 = new RequiredStringRule
				{
					InitialValue = "a",
					IgnoreCase = false,
					TrimWhiteSpace = true
				};
                Assert.IsFalse(rule1.IsEquivalent(rule2));

				rule1 = new RequiredStringRule
				{
					InitialValue = "a",
					IgnoreCase = true,
					TrimWhiteSpace = true
				};
				rule2 = new RequiredStringRule
				{
					InitialValue = " a ",
					IgnoreCase = true,
					TrimWhiteSpace = true
				};
                Assert.IsTrue(rule1.IsEquivalent(rule2));

				rule1 = new RequiredStringRule
				{
					InitialValue = "a",
					IgnoreCase = true,
					TrimWhiteSpace = true
				};
				rule2 = new RequiredStringRule
				{
					InitialValue = "b",
					IgnoreCase = true,
					TrimWhiteSpace = true
				};
                Assert.IsFalse(rule1.IsEquivalent(rule2));

            }
        }
        [TestFixture]
        public class Validate
        {
            [Test]
            public void NoInitialValueTrimSpacesIgnoreCase()
            {
                var rule = new RequiredStringRule
                           	{
                           		TrimWhiteSpace = true
                           	};

                Assert.IsFalse(rule.Validate(null, null, null, null));
                Assert.IsFalse(rule.Validate(null, string.Empty, null, null));
                Assert.IsFalse(rule.Validate(null, " ", null, null));
                Assert.IsTrue(rule.Validate(null, "s", null, null));
                Assert.IsTrue(rule.Validate(null, " s ", null, null));
                Assert.IsTrue(rule.Validate(null, "A", null, null));
                Assert.IsTrue(rule.Validate(null, " A ", null, null));

            }
            [Test]
            public void InitialValueTrimSpacesIgnoreCase()
            {
				var rule = new RequiredStringRule
				{
					InitialValue = "a",
					IgnoreCase = true,
					TrimWhiteSpace = true
				};

                Assert.IsTrue(rule.Validate(null, null, null, null));
                Assert.IsTrue(rule.Validate(null, string.Empty, null, null));
                Assert.IsTrue(rule.Validate(null, " ", null, null));
                Assert.IsTrue(rule.Validate(null, "s", null, null));
                Assert.IsTrue(rule.Validate(null, " s ", null, null));
                Assert.IsFalse(rule.Validate(null, "a", null, null));
                Assert.IsFalse(rule.Validate(null, "\t a ", null, null));
                Assert.IsFalse(rule.Validate(null, "A", null, null));
                Assert.IsFalse(rule.Validate(null, "\t A ", null, null));

            }

            [Test]
            public void InitialValueNoTrimSpacesIgnoreCase()
            {
				var rule = new RequiredStringRule
				{
					InitialValue = "a",
					IgnoreCase = true,
					TrimWhiteSpace = false
				};

                Assert.IsTrue(rule.Validate(null, null, null, null));
                Assert.IsTrue(rule.Validate(null, string.Empty, null, null));
                Assert.IsTrue(rule.Validate(null, " ", null, null));
                Assert.IsTrue(rule.Validate(null, "s", null, null));
                Assert.IsTrue(rule.Validate(null, " s ", null, null));
                Assert.IsFalse(rule.Validate(null, "a", null, null));
                Assert.IsTrue(rule.Validate(null, "\t a ", null, null));
                Assert.IsFalse(rule.Validate(null, "A", null, null));
                Assert.IsTrue(rule.Validate(null, "\t A ", null, null));

            }
            [Test]
            public void InitialValueTrimSpacesNoIgnoreCase()
            {
				var rule = new RequiredStringRule
				{
					InitialValue = "a",
					IgnoreCase = false,
					TrimWhiteSpace = true
				};

                Assert.IsTrue(rule.Validate(null, null, null, null));
                Assert.IsTrue(rule.Validate(null, string.Empty, null, null));
                Assert.IsTrue(rule.Validate(null, " ", null, null));
                Assert.IsTrue(rule.Validate(null, "s", null, null));
                Assert.IsTrue(rule.Validate(null, " s ", null, null));
                Assert.IsFalse(rule.Validate(null, "a", null, null));
                Assert.IsFalse(rule.Validate(null, "\t a ", null, null));
                Assert.IsTrue(rule.Validate(null, "A", null, null));
                Assert.IsTrue(rule.Validate(null, "\t A ", null, null));

            }

            [Test]
            public void InitialValueNoTrimSpacesNoIgnoreCase()
            {
				var rule = new RequiredStringRule
				{
					InitialValue = "a",
					IgnoreCase = false,
					TrimWhiteSpace = false
				};

                Assert.IsTrue(rule.Validate(null, null, null, null));
                Assert.IsTrue(rule.Validate(null, string.Empty, null, null));
                Assert.IsTrue(rule.Validate(null, " ", null, null));
                Assert.IsTrue(rule.Validate(null, "s", null, null));
                Assert.IsTrue(rule.Validate(null, " s ", null, null));
                Assert.IsFalse(rule.Validate(null, "a", null, null));
                Assert.IsTrue(rule.Validate(null, " a ", null, null));
                Assert.IsTrue(rule.Validate(null, "A", null, null));
                Assert.IsTrue(rule.Validate(null, " A ", null, null));

            }
            [Test]
            public void NoInitialValueNoTrimSpacesNoIgnoreCase()
            {
				var rule = new RequiredStringRule
				{
					IgnoreCase = false,
					TrimWhiteSpace = false
				};

                Assert.IsFalse(rule.Validate(null, null, null, null));
                Assert.IsFalse(rule.Validate(null, string.Empty, null, null));
                Assert.IsTrue(rule.Validate(null, " ", null, null));
                Assert.IsTrue(rule.Validate(null, "s", null, null));
                Assert.IsTrue(rule.Validate(null, " s ", null, null));
                Assert.IsTrue(rule.Validate(null, "a", null, null));
                Assert.IsTrue(rule.Validate(null, " a ", null, null));
                Assert.IsTrue(rule.Validate(null, "A", null, null));
                Assert.IsTrue(rule.Validate(null, " A ", null, null));
            }

            [Test]
            public void NoInitialValueTrimSpacesNoIgnoreCase()
            {
				var rule = new RequiredStringRule
				{
					IgnoreCase = false,
					TrimWhiteSpace = true
				};

                Assert.IsFalse(rule.Validate(null, null, null, null));
                Assert.IsFalse(rule.Validate(null, string.Empty, null, null));
                Assert.IsFalse(rule.Validate(null, " ", null, null));
                Assert.IsTrue(rule.Validate(null, "s", null, null));
                Assert.IsTrue(rule.Validate(null, " s ", null, null));
                Assert.IsTrue(rule.Validate(null, "a", null, null));
                Assert.IsTrue(rule.Validate(null, " a ", null, null));
                Assert.IsTrue(rule.Validate(null, "A", null, null));
                Assert.IsTrue(rule.Validate(null, " A ", null, null));

            }
        }
        [TestFixture]
        public class RuleInterpretation
        {

            [Test]
            public void Simple1()
            {
				var rule = new RequiredStringRule
				{
					InitialValue = "a",
					IgnoreCase = false,
					TrimWhiteSpace = true
				};
				Assert.IsNotNull(rule.RuleInterpretation);
            }
            [Test]
            public void Simple2()
            {
				var rule = new RequiredStringRule
				{
					InitialValue = "a",
					IgnoreCase = false,
					TrimWhiteSpace = false
				};
				Assert.IsNotNull(rule.RuleInterpretation);
            }
            [Test]
            public void Simple3()
            {
				var rule = new RequiredStringRule
				{
					InitialValue = "initialValue",
					IgnoreCase = true,
					TrimWhiteSpace = true
				};
                Assert.IsNotNull(rule.RuleInterpretation);
            }

        }
        [TestFixture]
        public class CheckTypes
        {
            [Test]
            public void Simple()
            {
            	var rule = new RequiredStringRule();
                rule.CheckType(new MockInfoDescriptor(TypePointers.StringTypeHandle, "foo"));
            }


            [Test]
            [ExpectedException(typeof(ArgumentException), ExpectedMessage = "Member 'foo' must be a 'System.String' to be used for the ValidationFramework.RequiredStringRule. Actual Type 'System.Int32'.\r\nParameter name: value")]
            public void ExceptionInvalidType()
            {
            	var rule = new RequiredStringRule();
                        rule.CheckType(new MockInfoDescriptor(TypePointers.IntTypeHandle, "foo"));
            }
        }
    }
}